/*----------------------------------------------
| noka top button v1.0 www.97521.com           |
| rebin 2016-07-23                             |
|---------------------------------------------*/
var nokatop = Class.create({
	version: '1.0',
	initialize : function(cfg) {
	   this.id = cfg.id;
	   this.menus=cfg.menus || [] ;//菜单    [{name:'',width:'',id:'',url:'',target:'',onclick:function(nemu){},sub:[]}]
	   this.menulist=[];
	   this.onclick = cfg.onclick;
	   this.keepstate= (undefined == cfg.keepstate?false:cfg.keepstate);//保持状态
	   this.direction = cfg.direction;//right | left
	},
	show : function(){
	    this.createMenu();
	    this.initMenu();
	},
	//---------------创建下拉菜单-------------------
	createMenu:function(){
		var self = this;
		var idx=0;
		var html=[];
		var menus = self.menus;
		for(var i=0;i<menus.length;i++){
			var width = (undefined!=menus[i].width?'width:'+menus[i].width+'px':'');
			if(undefined!=menus[i].sub && menus[i].sub.length>0){
				html[idx++]='<div id="'+self.id+'_user_button_'+i+'" class="noka_top_select" style="'+width+';overflow:auto">';
				html[idx++]='<span>'+menus[i].name+'</span><span class="noka_top_select_img_down" style="float: right;"></span>';
				html[idx++]='</div>';
				html[idx++]='<div id="'+self.id+'_user_list_'+i+'" class="noka_top_list" style="left:0px;top:0px;display: none;z-index: 9999;position:absolute;">';
				html[idx++]=self.createSubMenu(i,menus[i].sub);
				html[idx++]='</div>';
			}else if(undefined!=menus[i].scent){
				html[idx++]='<div id="'+self.id+'_user_button_'+i+'" class="noka_top_select" style="'+width+'; style="float: right;"">';
				html[idx++]='<span>'+menus[i].name+'</span><span class="noka_top_select_img_down"></span>';
				html[idx++]='</div>';
				html[idx++]='<div id="'+self.id+'_user_list_'+i+'" class="noka_top_list" style="left:0;display: none;z-index: 9999;position:absolute;">';
				html[idx++]=$(menus[i].scent).innerHTML;
				html[idx++]='</div>';
				$(menus[i].scent).remove();
			}else{
				html[idx++]='<div id="'+self.id+'_user_button_'+i+'" class="noka_top_select" style="'+width+' style="float: right;"">';
				html[idx++]='<span>'+menus[i].name+'</span>';
				html[idx++]='</div>';
			}
			self.menulist[self.id+'_user_json_'+i]=menus[i];
			self.menus[i].id=self.id+'_user_button_'+i;
		}
		html[idx++]='<a id="'+self.id+'_user_alink" href="" target="" style="display:none;"></a>';
		$(self.id).innerHTML=html.join('');
		self.selectEvent();
		self.subMenuEvent();
	},
	//------------------------------------------------------
	selectEvent : function(){
		var self = this;
		var miFlg = {};
		$$('div[id^="'+self.id+'_user_button_"]').each(function(alink,index) {
			var i=self.getnemuno($(alink).id);
			var vlist =$(self.id+'_user_list_'+i);
			$(alink).observe('mousemove', function() {
				self.setSelectStyle($(alink));
				miFlg[$(alink).id]=true;
			});
			$(alink).observe('mouseout', function(event) {
				miFlg[$(alink).id] = false;
				var element = event.element();
	        	setTimeout(function(){
	                if (element.descendantOf($(self.id)) && !miFlg[$(alink).id]){
	                	self.setNoSelectStyle($(alink));
	                	if(undefined!=vlist && undefined!=vlist.id){
	                		vlist.hide();
	                	}
	                	$(alink).removeClassName('noka_top_select_open');
	                	$(alink).addClassName('noka_top_select');
	                }
	            }, 500);
			});
			$(alink).observe('click', function() {
				$(alink).removeClassName('noka_top_select');
            	$(alink).addClassName('noka_top_select_open');
            	//-----------------------------------------------------
            	if(undefined!=vlist && undefined!=vlist.id){//有子菜单
	            	setTimeout(function(){
	            		self.showList($(alink).id);
	            	},100);
            	}
            	//-----------------------------------------------------
            	var menujson = self.menulist[self.id+'_user_json_'+i];
            	self.menuutil(menujson);
            	//-----------------------------------------------------
            	if(self.keepstate){//记录点击的一级菜单
            		setCookie('ntopmenu_1',$(alink).id);
            	}
			});
			if(undefined!=vlist && undefined!=vlist.id){//有子菜单
				vlist.observe('mousemove', function() {
					self.setSelectStyle($(alink));
					miFlg[$(alink).id] = true;
				});
				vlist.observe('mouseout', function(event) {
					miFlg[$(alink).id] = false;
					var element = event.element();
		        	setTimeout(function(){
		                if (element.descendantOf($(self.id)) && !miFlg[$(alink).id]){
		                	self.setNoSelectStyle($(alink));
		                	vlist.hide();
		                	$(alink).removeClassName('noka_top_select_open');
		                	$(alink).addClassName('noka_top_select');
		                }
		            }, 500);
				});
			}
		});
	},
	//---------------------------------------
	menuutil : function(menujson){
		var self = this;
		if(undefined!=menujson.onclick){
    		try{
    			menujson.onclick(menujson);
    		}catch(e){}
    	}
    	if(undefined!=menujson.url && menujson.url!=''){
    		$(self.id+'_user_alink').href=menujson.url;
    		if(undefined!=menujson.target && menujson.target!=''){
    			$(self.id+'_user_alink').target = menujson.target;
    		}
    		$(self.id+'_user_alink').click();
    	}
    	if(undefined!=self.onclick){
    		try{
    			self.onclick(menujson);
    		}catch(e){}
    	}
	},
	//----------选种状态-----------------------
    setSelectStyle : function(obj){
    	obj.removeClassName('noka_top_select');
    	obj.addClassName('noka_top_select_hover');
    },
    //-----------未选种状态-------------------
    setNoSelectStyle : function(obj){
    	obj.removeClassName('noka_top_select_hover');
    	obj.addClassName('noka_top_select');
    },
	//-------------------------------------------------------
	createSubMenu : function(a,menus){
		var self = this;
		var idx=0;
		var html=[];
		for(var i=0;i<menus.length;i++){
			var width = (undefined!=menus[i].width?'width:'+menus[i].width+'px':'');
			html[idx++]='<div id="'+self.id+'_user_subbutton_'+i+'T'+a+'" style="'+width+'" class="noka_top_bodylist">';
			html[idx++]='<span sytle="display:inline-block;font-size:12px;">'+menus[i].name+'</span>';
			html[idx++]='</div>';
			self.menulist[self.id+'_user_subjson_'+i+'T'+a]=menus[i];
		}
		return html.join('');
	},
	//------------------------------------------------------
	subMenuEvent : function(){
		var self = this;
		$$('div[id^="'+self.id+'_user_subbutton_"]').each(function(alink,index) {
			var i=self.getnemuno($(alink).id);
			$(alink).observe('click', function() {
				var menujson = self.menulist[self.id+'_user_subjson_'+i];
            	self.menuutil(menujson);
            	if(self.keepstate){//记录点击的二级菜单
            		setCookie('ntopmenu_2',$(alink).id);
            	}
			});
		});
	},
	getnemuno : function(muid){
		if(undefined!=muid){
			var ms = muid.split('_');
			var len = ms.length-1;
			return ms[len];
		}
		return;
	},
	showList : function(id){
		var self = this;
		var i = self.getnemuno(id);
		var listBody = $(self.id+'_user_list_'+i);
		var obj = $(id);
		var lefts=obj.positionedOffset().left;
		var top=obj.offsetTop+obj.getHeight();
		//-----------------------------------------------------
		if(listBody.getWidth()<obj.getWidth()){
			try{
				listBody.setStyle({width:obj.getWidth()+'px'});
				listBody.Style.width=obj.getWidth()+'px';
			}catch(e){}
		}
		//-----------------------------------------------------
		if('right'==self.direction){
			lefts=(lefts+obj.getWidth()) - listBody.getWidth();
		}
		listBody.setStyle({left:(lefts)+'px',top:top+'px'});
		listBody.show();
	},
	//------------初始化菜单----------------------------------------
	initMenu : function(){
		var self = this;
		if(self.keepstate){//记录点击的二级菜单
			var topmenu_1 = getCookie('ntopmenu_1');
			var topmenu_2 = getCookie('ntopmenu_2');
			if(undefined!=topmenu_2){//有二级
				$(topmenu_2).click();
			}else if(undefined!=topmenu_1){
				$(topmenu_1).click();
				self.setNoSelectStyle($(topmenu_1));
				$(topmenu_1).removeClassName('noka_top_select_open');
                $(topmenu_1).addClassName('noka_top_select');
			}
		}
	}
});